﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using netcore_comm.Redis;
using netcore_comm.SqlSugar;
using netcore_service.IRepository;
using netcore_service.IService;
using netcore_service.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace netcore_service.Service
{

    public class MeetingService : IMeetingService
    {
        private readonly ISqlSugarFactory _sqlSugarFactory;
        private readonly RedisHelper _redisHelper;
        private readonly IMeetingRepository _meetingRepository;
        public MeetingService(ISqlSugarFactory sqlSugarFactory, RedisHelper redisHelper, IMeetingRepository meetingRepository)
        {
            _sqlSugarFactory = sqlSugarFactory;
            _redisHelper = redisHelper;
            _meetingRepository = meetingRepository;
        }
        [Authorize]
        [HttpPost]
        public async Task<ApiResult<List<MeetInfo>>> MeetListAsync()
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return APIResult.GetSuccess<List<MeetInfo>>(await _meetingRepository.GetListAsync(db));
        }

        [HttpPost]
        public async Task<ApiResult<MeetInfo>> MeetListOne()
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            var list = await _meetingRepository.GetListAsync(db);
            return APIResult.GetSuccess<MeetInfo>(list.First());
        }


        [Authorize]
        [HttpPost]
        public async Task<ApiResult<bool>> MeetUpdateOrAddAsync(MeetInfo meetInfo)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);
            if (meetInfo.Id == 0)
            {
                meetInfo.ModifyDate = DateTime.Now;
                return APIResult.GetSuccess<bool>(await _meetingRepository.InsertAsync(db, meetInfo));
            }
            else
            {
                return APIResult.GetSuccess<bool>(await _meetingRepository.UpdateAsync(db, meetInfo));
            }
        }
        [Authorize]
        [HttpPost]
        public async Task<ApiResult<MeetInfo>> MeetByIdAsync(int id)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return APIResult.GetSuccess<MeetInfo>(await _meetingRepository.GetModelAsync(db, e => e.Id == id));
        }
        [Authorize]
        [HttpPost]
        public async Task<ApiResult<bool>> MeetDelAsync(int id)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);
            return APIResult.GetSuccess<bool>(await _meetingRepository.DeleteAsync(db, e => e.Id == id));
        }

    }
}
